<?
// /ui/commandhandler/request/group|renew|restore/group/[groupname][/status/ok]

class News_Command_ArticleFinder
{
    var $Controller;
    var $Group; 
    var $Request; 
    var $Find; 
    var $Seek; 
    function News_Command_ArticleFinder($controller)
	{
	    $this->Controller = $controller;
        $this->Group      = $this->Controller->Settings['group'];  
        $this->Request    = $this->Controller->Settings['request'];  
        $this->Find       = $this->Controller->Settings['find']; 
        $this->Seek       = $this->Controller->Settings['seek']; 
        $this->Path       = "notify/{$this->Group}.{$this->Find}." . $this->Request;
        $this->URI        = $this->Controller->URI (array('status'=>'ok', $this->Request=>$this->Find));
	}
	
	function GetURI()
	{
	    $uri = array('ui'=>'articlelist', 'request'=>NULL, $this->Request=>$this->Find); 
		if (isset($this->Seek))
		{//http://www2.ghost.net/ui/commandhandler/group/alt.binaries.pictures.bluebird.reposts/seek/onica/request/page/page/2 
			
	        $uri = array('ui'=>'commandhandler', 'request'=>'page', 'group'=>$this->Group); 
		 
			$uri['seek']   = $this->Find;
			$uri['page']    = 1;
		}		 
		$uri = $this->Controller->URI ($uri);		
		 
	    return array("RequestURI"  => $this->URI,
	                 "RedirectURI" => $uri);
	}
	
	function GetStatus()
	{
	    if (!file_exists($this->Path))
        {
            return 'OK';
        } 
        return '';
	}
	
	function Display()
	{  
        $side   = $this->Controller->Settings['side'];  
        $index  = $this->Controller->Settings['index'];
        $pic    = $this->Controller->Settings['pic'];
        $idx    = "data/temp/{$side}{$index}.jpeg.idx"; 
        
        
        if (file_exists($idx) && !isset($pic)) $pic = $this->Controller->Read($idx);
        
        if (isset($pic))
        {
            
            if (file_exists($pic))
            {
                $tmp = explode ('/', $pic);
                $key = base64_decode(array_pop($tmp));
                
                
                
                $grp = array_pop($tmp);
                $loc = "data/{$grp}.xml";
                $htm = "<img height='400' src='/{$pic}'/><br>";
                $xml = simplexml_load_file($loc);
                $sql = "//article[contains(id,'{$key}')]";
                $ask = "/ui/commandhandler/request/group/group/{$grp}";
                $ele = $xml->xpath($sql);  
                $fn  = 0;
                $swp = NULL;
                while (sizeof($ele)==0)
                {
                    $fn ++;
//                    {
                         $tmp = "data/{$grp},{$fn}.xml";
                         if (file_exists($tmp))
                         {
                             $xml = simplexml_load_file($tmp); 
                             $swp = $tmp;
                             $ele = $xml->xpath($sql);
                         } else break;
//                    } 
                    
                }
                if (sizeof ($ele) > 0)
                {
                    if ($swp)
                    { 
                        rename ($loc, $loc.'.tmp');
                        rename ($swp, $loc);
                        rename ($loc.'.tmp', $swp);
                    }
                    
                    $htm .= sprintf("<div>%s</div>", $ele[0]->subject);
                    $pop = $xml->xpath("{$sql}/..");  
                    $was = $pop[0]->getName();
                    $xpath = sprintf("{$sql}/%s",$was =='items'?"../..":".");
                    $iroot = $xml->xpath($xpath);  
                    if (sizeof ($iroot)>0)
                    {
                        $art = $iroot[0];
			            $att     = $art->attributes();
			            $index   = $att['index'];
	                    $page    = floor ($index / UI_PAGE_SIZE) + 1 ;
			            $uri     = array ('ui'      => 'commandhandler', 
			                              'request' => 'index', 
			                              'index'   => $index, 
			                              'page'    => $page, 
			                              'group'   => $grp, 
			                              'item'    => $art->id,
			                              'side'    => NULL
			                             );
			            $browse  = array ('ui'      => 'commandhandler', 
			                              'request' => 'browse', 
			                              'group'   => $grp, 
			                              'page'    => $page, 
			                              'pane'    => 1, 
			                              'index'   => $index ,
			                              'side'    => NULL
			                             );
			                    
			            $browse  = $this->Controller->Factory->Button($this->Controller->URI ($browse), 'Browse','i.p.folder.drafts.gif');  
			            $browse .= $this->Controller->Factory->Button($this->Controller->URI ($uri), 'Open','i.p.edit.gif') . '<br/>';  
			                             
                        $htm .= $browse;
                    }
//                    echo $was;
                }
                else $htm .= "Could not find $key in $loc<br/>";
                $htm .= $this->Controller->Factory->Button($ask, 'Open '.$grp,'address-go.gif');  
                echo $htm;
                return;
            }
            echo "Could not locate picture {$pic}";
            return;
        }
        echo "Could not locate index file {$idx}";
	}
	
	function Signal()
	{  
	    if ($this->Request=='lookup')
	    {
	        $this->Display();
	        return;
        }
        $message =  "{$this->Group}";
        
		if (isset($this->Seek))
		{
            $message = "groupname={$this->Group}&filename=data/find/{$this->Group}/{$this->Find}.xml";
		}		 
				 
        if (strlen($this->Controller->Settings['status']) > 0)
        {
            return $this->GetStatus ();
        } 
        
        $this->Controller->Save($this->Path, $message); 
        return $this->Controller->Dialog ($this->GetURI());
	}
	
	function Read($filename, $findparam, $groupname)
	{  
        if (file_exists($filename)) 
        {
		    $xml = simplexml_load_file($filename);
		    if ($xml)
		    {
                $this->Controller->OpenConnection($groupname); 
			    $result = $xml->xpath("//list/article[contains(subject, '{$findparam}')]");  
			    $job = 0;
			    for($x=0;$x<sizeof($result);$x++)
			    { 
			        $job ++;
			        $id=$result[$x]->id;
			        $type=$result[$x]->type;
			        echo "Job #{$job}\n------------------------------------\n";
			        if ((!isset($type)) || $type=='' || $type=='picture')
			        {
			            echo $id . "\n"; 
			            $picname = $this->Controller->GetArticle ($groupname, $id);
			            echo $picname . "\n"; 
			        }
		            $this->Controller->LogEntry ("Loading results {$findparam}", $job, sizeof($result), $groupname);
			    }
                $this->Controller->Imager->Disconnect();
		    } 
        }   
	}
	// http://www2.ghost.net/ui/commandhandler/page/1/request/find/group/alt.binaries.pictures.candydoll
	function Invoke($command)
	{   
	    $this->Path = $command['path']; 
        $findparam  = $command['arg1'];
        $groupname  = $this->Controller->Read($command['path']); 
		if (strpos($groupname, '&')) 
		{
			parse_str($groupname); 
		     $file = $this->Controller->MassiveSearch($groupname, $findparam);  
		}
		else 
		{ 
            $filename   = "data/{$groupname}.xml";
            $this->Read($filename, $findparam, $groupname); 
		}
		unlink ($this->Path); 
	}
     
}



